#!/usr/bin/python
# -*-coding:UTF-8 -*-

# ========================
# @Time  : 2020/7/20
# @Author: Sunny
# ========================

import re
import json
import os

from common.yaml_packaging import YamlHandle
from common.path_constants import APPLICATION_RECORD_YAML, ATTENDANCE_PATTERN_CONF,ATTENDANCE_SQL_CONF
from common.mysql_packaging import MysqlHandle
from common.config_packaging import ConfigHandle
from parameterize.user_param import UserContextHandle
from common.context_packaging import ContextHandle
from data.com_function import time_after,generate_random_no,select_random_item,day_after

class AttendanceContextHandle(ContextHandle):
    """
    定义一个考勤信息参数化的类
    """
    sql_config = ConfigHandle(ATTENDANCE_SQL_CONF)
    attendance_re_config = ConfigHandle(ATTENDANCE_PATTERN_CONF)


    @classmethod
    def rep_overtime_applied_time_info(cls,data):
        """
        替换加班时间信息(考勤开始时间和结束时间)
        :param data:
        :return:
        """
        appliedTimeStart_pattern = re.compile(eval(cls.attendance_re_config("attendance","applied_time_start")))
        appliedTimeEnd_pattern = re.compile(eval(cls.attendance_re_config("attendance", "applied_time_end")))
        if re.search(appliedTimeStart_pattern, data):
            data = re.sub(appliedTimeStart_pattern, day_after(3)+" "+"08:30:00", data)
        if re.search(appliedTimeEnd_pattern, data):
            data = re.sub(appliedTimeEnd_pattern, day_after(3)+" "+"12:00:00", data)
        return data

    @classmethod
    def rep_missed_clock_time_info(cls,data):
        """
        替换漏打卡时间信息
        :param data:
        :return:
        """
        missed_clock_time_pattern = re.compile(eval(cls.attendance_re_config("attendance","missed_clock_time")))
        if re.search(missed_clock_time_pattern, data):
            data = re.sub(missed_clock_time_pattern, day_after(-2)+" "+"08:30:00", data)
        return data

    @classmethod
    def rep_job_number_id_with_year_holiday(cls,data):
        """
        替换存在年假的员工user信息
        :param data:
        :return:
        """
        sql_obj = MysqlHandle()
        job_number_with_year_holiday_pattern = re.compile(eval(cls.attendance_re_config("attendance", "job_number_with_year_holiday")))
        user_id_with_year_holiday_pattern = re.compile(eval(cls.attendance_re_config("attendance", "user_id_with_year_holiday")))
        holiday_year_pattern = re.compile(eval(cls.attendance_re_config("attendance", "holiday_year")))
        user_data = sql_obj(sql=cls.sql_config("attendance","user_info_with_year_holiday"))
        if re.search(job_number_with_year_holiday_pattern, data):
            data = re.sub(job_number_with_year_holiday_pattern, str(user_data['job_number']), data)
        if re.search(user_id_with_year_holiday_pattern, data):
             data = re.sub(user_id_with_year_holiday_pattern, str(user_data['user_id']), data)
        if re.search(holiday_year_pattern,data):
             data = re.sub(holiday_year_pattern,str(user_data['year_holiday']),data)
        sql_obj.close_sql()
        return data

    @classmethod
    def rep_user_info_with_overtime(cls,data):
        """
        替换有历史加班单的员工user信息
        :param data:
        :return:
        """
        sql_obj = MysqlHandle()
        job_number_with_overtime_pattern = re.compile(eval(cls.attendance_re_config("attendance", "job_number_with_overtime")))
        user_id_with_overtime_pattern = re.compile(eval(cls.attendance_re_config("attendance", "user_id_with_overtime")))
        source_overtime_time_start_pattern = re.compile(eval(cls.attendance_re_config("attendance", "source_overtime_time_start")))
        source_overtime_time_end_pattern = re.compile(eval(cls.attendance_re_config("attendance", "source_overtime_time_end")))
        form_id_pattern = re.compile(eval(cls.attendance_re_config("attendance", "overtime_form_id")))
        rest_num_1_pattern = re.compile(eval(cls.attendance_re_config("attendance", "rest_num_1")))
        rest_num_2_pattern = re.compile(eval(cls.attendance_re_config("attendance", "rest_num_2")))
        user_data = sql_obj(sql=cls.sql_config("attendance","user_info_with_overtime"))
        if re.search(job_number_with_overtime_pattern, data):
            data = re.sub(job_number_with_overtime_pattern, str(user_data['applied_job_number']), data)
        if re.search(user_id_with_overtime_pattern, data):
             data = re.sub(user_id_with_overtime_pattern, str(user_data['user_id']), data)
        if re.search(source_overtime_time_start_pattern,data):
            data = re.sub(source_overtime_time_start_pattern, str(user_data['applied_time_start']), data)
        if re.search(source_overtime_time_end_pattern,data):
            data = re.sub(source_overtime_time_end_pattern, str(user_data['applied_time_end']), data)
        if re.search(form_id_pattern,data):
            data = re.sub(form_id_pattern,str(user_data['form_id']),data)
        if re.search(rest_num_1_pattern, data):
            data = re.sub(rest_num_1_pattern, str(int(user_data['holiday_num'])), data)
        if re.search(rest_num_2_pattern, data):
            data = re.sub(rest_num_2_pattern, str(int(user_data['holiday_num'])/2), data)
        sql_obj.close_sql()
        return data


    @classmethod
    def rep_user_info_with_personal_leave(cls, data):
        """
        替换有事假的员工user信息及事假申请时间、结束时间
        :param data:
        :return:
        """
        sql_obj = MysqlHandle()
        job_number_with_personal_leave_pattern = re.compile(eval(cls.attendance_re_config("attendance", "job_number_with_personal_leave")))
        user_id_with_personal_leave_pattern = re.compile(eval(cls.attendance_re_config("attendance", "user_id_with_personal_leave")))
        source_applied_time_start_pattern = re.compile(eval(cls.attendance_re_config("attendance", "source_applied_time_start")))
        source_applied_time_end_pattern = re.compile(eval(cls.attendance_re_config("attendance", "source_applied_time_end")))
        # 有事假的员工和单据信息
        user_data = sql_obj(sql=cls.sql_config("attendance", "user_info_with_different_holiday"),args=('06',))
        if re.search(job_number_with_personal_leave_pattern, data):
            data = re.sub(job_number_with_personal_leave_pattern, str(user_data['applied_job_number']), data)
        if re.search(user_id_with_personal_leave_pattern, data):
            data = re.sub(user_id_with_personal_leave_pattern, str(user_data['user_id']), data)
        if re.search(source_applied_time_start_pattern, data):
            data = re.sub(source_applied_time_start_pattern, str(user_data['applied_time_start']), data)
        if re.search(source_applied_time_end_pattern, data):
            data = re.sub(source_applied_time_end_pattern, str(user_data['applied_time_end']), data)
        sql_obj.close_sql()
        return data

    @classmethod
    def rep_user_info_with_rest_leave(cls, data):
        """
        替换有调休单的员工user信息及事假申请时间、结束时间
        :param data:
        :return:
        """
        sql_obj = MysqlHandle()
        job_number_with_rest_leave_pattern = re.compile(eval(cls.attendance_re_config("attendance", "job_number_with_rest_leave")))
        user_id_with_rest_leave_pattern = re.compile(eval(cls.attendance_re_config("attendance", "user_id_with_rest_leave")))
        source_rest_time_start_pattern = re.compile(eval(cls.attendance_re_config("attendance", "source_rest_time_start")))
        source_rest_time_end_pattern = re.compile(eval(cls.attendance_re_config("attendance", "source_rest_time_end")))
        form_id_pattern = re.compile(eval(cls.attendance_re_config("attendance","rest_form_id")))
        # 有调休单据的员工信息和单据信息
        user_data = sql_obj(sql=cls.sql_config("attendance", "user_info_with_different_holiday"),args=('05',))
        if re.search(job_number_with_rest_leave_pattern, data):
            data = re.sub(job_number_with_rest_leave_pattern, str(user_data['applied_job_number']), data)
        if re.search(user_id_with_rest_leave_pattern, data):
            data = re.sub(user_id_with_rest_leave_pattern, str(user_data['user_id']), data)
        if re.search(source_rest_time_start_pattern, data):
            data = re.sub(source_rest_time_start_pattern, str(user_data['applied_time_start']), data)
        if re.search(source_rest_time_end_pattern, data):
            data = re.sub(source_rest_time_end_pattern, str(user_data['applied_time_end']), data)
        if re.search(form_id_pattern,data):
            data = re.sub(form_id_pattern,str(user_data['form_id']),data)
        sql_obj.close_sql()
        return data

    @classmethod
    def rep_user_info_with_sick_leave(cls, data):
        """
        替换有病假单的员工user信息及事假申请时间、结束时间
        :param data:
        :return:
        """
        sql_obj = MysqlHandle()
        job_number_with_sick_leave_pattern = re.compile(eval(cls.attendance_re_config("attendance", "job_number_with_sick_leave")))
        user_id_with_sick_leave_pattern = re.compile(eval(cls.attendance_re_config("attendance", "user_id_with_sick_leave")))
        source_sick_time_start_pattern = re.compile(eval(cls.attendance_re_config("attendance", "source_sick_time_start")))
        source_sick_time_end_pattern = re.compile(eval(cls.attendance_re_config("attendance", "source_sick_time_end")))
        # 有调休单据的员工信息和单据信息
        user_data = sql_obj(sql=cls.sql_config("attendance", "user_info_with_different_holiday"),args=('07',))
        if re.search(job_number_with_sick_leave_pattern, data):
            data = re.sub(job_number_with_sick_leave_pattern, str(user_data['applied_job_number']), data)
        if re.search(user_id_with_sick_leave_pattern, data):
            data = re.sub(user_id_with_sick_leave_pattern, str(user_data['user_id']), data)
        if re.search(source_sick_time_start_pattern, data):
            data = re.sub(source_sick_time_start_pattern, str(user_data['applied_time_start']), data)
        if re.search(source_sick_time_end_pattern, data):
            data = re.sub(source_sick_time_end_pattern, str(user_data['applied_time_end']), data)
        sql_obj.close_sql()
        return data

    @classmethod
    def application_rest_info_parameterize(cls,data):
        """
        申请调休信息参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = cls.rep_job_number_id_with_year_holiday(data)
        data = cls.rep_overtime_applied_time_info(data)
        data = cls.rep_user_info_with_overtime(data)
        return data

    @classmethod
    def rep_copy_emp_id_info(cls,data):
        """
        替换考勤申请抄送人信息
        :param data:
        :return:
        """
        sql_obj = MysqlHandle()
        copy_emp_id_pattern = re.compile(eval(cls.attendance_re_config("attendance","copy_emp_id")))
        if re.search(copy_emp_id_pattern,data):
            copy_emp_id = sql_obj(sql=cls.sql_config("attendance","copy_emp_id"),args=("008770",))['emp_id']
            data = re.sub(copy_emp_id_pattern,str(copy_emp_id),data)
        sql_obj.close_sql()
        return data


    @classmethod
    def application_overtime_info_parameterize(cls,data):
        """
        申请加班信息参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = UserContextHandle.emp_user_info_parameterize(data)
        data = cls.rep_overtime_applied_time_info(data)
        data = cls.rep_copy_emp_id_info(data)
        data = cls.rep_missed_clock_time_info(data)
        return data

    @classmethod
    def application_cancel_leave_parameterize(cls,data):
        """
        申请销假信息参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = cls.rep_user_info_with_personal_leave(data)
        data = cls.rep_overtime_applied_time_info(data)
        data = cls.rep_user_info_with_rest_leave(data)
        data = cls.rep_user_info_with_sick_leave(data)
        data = cls.rep_user_info_with_overtime(data)
        return data


    @classmethod
    def application_vacation_to_salary_parameterize(cls,data):
        """
        申请调休转薪资信息参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = cls.rep_job_number_id_with_year_holiday(data)
        data = cls.rep_user_info_with_overtime(data)
        return data

    @classmethod
    def rep_department_info(cls,data):
        """
        替换部门信息
        :param data:
        :return:
        """
        sql_obj = MysqlHandle()
        dpt_id_pattern = re.compile(eval(cls.attendance_re_config("attendance","dpt_id")))
        dept_id_data = sql_obj(sql=cls.sql_config("attendance", "dpt_id"), args=('75',),fetch_all=True)
        if re.search(dpt_id_pattern, data):
            ids = ''
            for i in range(0, len(dept_id_data)):
                ids = ids + str(dept_id_data[i]['department_id']) + ','
            data = re.sub(dpt_id_pattern, str(ids), data)
        sql_obj.close_sql()
        return data


    @classmethod
    def rep_attendance_creat_time_info(cls,data):
        """
        搜索栏考勤开始时间及申请时间信息替换
        :param data:
        :return:
        """
        from_date_pattern = re.compile(eval(cls.attendance_re_config("attendance","from_date")))
        to_date_pattern = re.compile(eval(cls.attendance_re_config("attendance", "to_date")))
        if re.search(from_date_pattern, data):
            data = re.sub(from_date_pattern, day_after(-60)+" "+"00:00:00", data)
        if re.search(to_date_pattern, data):
            data = re.sub(to_date_pattern, day_after(-30)+" "+"23:59:59", data)
        return data

    @classmethod
    def rep_exist_form_and_num__info(cls,data):
        """
        已存在的单据编号和工号信息替换
        :param data:
        :return:
        """
        sql_obj = MysqlHandle()
        form_id_pattern = re.compile(eval(cls.attendance_re_config("attendance","form_id")))
        applied_job_number_pattern = re.compile(eval(cls.attendance_re_config("attendance", "applied_job_number")))
        data_list = sql_obj(sql=cls.sql_config("attendance", "exist_form_and_num"))
        if re.search(form_id_pattern, data):
            data = re.sub(form_id_pattern,str(data_list['form_id']), data)
        if re.search(applied_job_number_pattern, data):
            data = re.sub(applied_job_number_pattern,str(data_list['applied_job_number']), data)
        sql_obj.close_sql()
        return data

    @classmethod
    def rep_exist_more_form_info(cls,data):
        """
        已存在的多个考勤编号信息
        :param data:
        :return:
        """
        sql_obj = MysqlHandle()
        form_id_1_pattern = re.compile(eval(cls.attendance_re_config("attendance","form_id_1")))
        form_id_2_pattern = re.compile(eval(cls.attendance_re_config("attendance", "form_id_2")))
        data_list = sql_obj(sql=cls.sql_config("attendance", "exist_form_and_num"),fetch_all=True)
        print(data_list[0]['form_id'],data_list[1]['form_id'])
        if re.search(form_id_1_pattern, data):
            data = re.sub(form_id_1_pattern,str(data_list[0]['form_id']), data)
        if re.search(form_id_2_pattern, data):
            data = re.sub(form_id_2_pattern,str(data_list[1]['form_id']), data)
        sql_obj.close_sql()
        return data

    @classmethod
    def application_record_search_info_parameterize(cls,data):
        """
        申请历史查询页面信息参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = UserContextHandle.attendance_user_info_parameterize(data)
        data = cls.rep_department_info(data)
        data = cls.rep_attendance_creat_time_info(data)
        data = cls.rep_exist_form_and_num__info(data)
        return data


    @classmethod
    def application_record_export_info_parameterize(cls,data):
        """
        申请记录页面导出信息参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = UserContextHandle.attendance_user_info_parameterize(data)
        data = cls.rep_department_info(data)
        data = cls.rep_attendance_creat_time_info(data)
        data = cls.rep_exist_more_form_info(data)
        return data






if __name__ == '__main__':
    yaml_obj = YamlHandle(APPLICATION_RECORD_YAML)
    data = yaml_obj.get_data("application_record_export")
    data = data[2]
    print(type(data),data)
    # data = json.dumps(data)
    data = AttendanceContextHandle.application_record_export_info_parameterize(data)
    print(type(data),data)
    # data = json.loads(data)
    # print(type(data), data)
    # print(str(os.path.join(PICTURE_DIR,'black.jpg')))
    # print(type(os.path.join(PICTURE_DIR,'black.jpg')))

